/**
 * 
 */
package com.demo2do.fms.admin.entity.search.data;

import java.util.List;
import java.util.Map;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 * 
 * @author Chang Liu
 *
 */
public class FacilityDistanceSearch implements QueryBuilder {
    
    /** 选择的公司 */
    private List<Long> organizationIds;
    
    /** 起始地 */
    private Long fromLoadPlaceId;
    
    /** 目的地 */
    private Long toLoadPlaceId;
    
    private QueryFilter filter;
    
    private QueryOrder order;

    /**
     * 
     * the default constructor
     */
    public FacilityDistanceSearch() {
        order = new QueryOrder().add("organization.name", "ASC")
                                .add("fromLoadPlace.name", "ASC");
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if(filter == null || refresh){
            
            filter = new QueryFilter().addEquals("fromLoadPlace.id", fromLoadPlaceId)
                                      .addEquals("toLoadPlace.id", toLoadPlaceId)
                                      .addSentence("organization.id IN (:organizationIds)", "organizationIds", organizationIds);
                
        }
        
        return filter;
        
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return order;
    }
    
    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        String sentence = "FROM FacilityDistance WHERE disabled = false" + getPreparedFilter(false).clauseSentence() + order.sentence();
        return sentence;
    }

    /*
     * (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }

    /**
     * 
     * @return
     */
    public QueryFilter getFilter(){
        return getPreparedFilter(false);
    }
    
    /**
     * @return the organizationIds
     */
    public List<Long> getOrganizationIds() {
        return organizationIds;
    }

    /**
     * @return the fromLoadPlaceId
     */
    public Long getFromLoadPlaceId() {
        return fromLoadPlaceId;
    }

    /**
     * @return the toLoadPlaceId
     */
    public Long getToLoadPlaceId() {
        return toLoadPlaceId;
    }
    
    /**
     * @param organizationIds the organizationIds to set
     */
    public void setOrganizationIds(List<Long> organizationIds) {
        this.organizationIds = organizationIds;
    }

    /**
     * @param fromLoadPlaceId the fromLoadPlaceId to set
     */
    public void setFromLoadPlaceId(Long fromLoadPlaceId) {
        this.fromLoadPlaceId = fromLoadPlaceId;
    }

    /**
     * @param toLoadPlaceId the toLoadPlaceId to set
     */
    public void setToLoadPlaceId(Long toLoadPlaceId) {
        this.toLoadPlaceId = toLoadPlaceId;
    }
    
}
